iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 7
0
Data Technology

Microsoft R Solution系列 第 7

Day07 - Under fitting與 Overfitting Part2

  • 分享至 

  • xImage
  •  

接續上一篇的結論, y=b0+b1X的預測效果不是很好, 甚至Under fitting了, 所以回到EDA的步驟, 試著把所有點連起看看, 修改程式碼如下
https://ithelp.ithome.com.tw/upload/images/20171225/201033334zrI6GqvBc.png
https://ithelp.ithome.com.tw/upload/images/20171225/20103333MX1XcswHRy.png
觀察上圖後發現, 好像一條曲線而非直線, 所以應該可以用y=b0+b1X +b2X^2來試試, 由上一篇了解到X指的是職等即Level, 所以我們可以新增一個X^2的資料, 即加入Level平方的資料進來, 修改程式碼如下
https://ithelp.ithome.com.tw/upload/images/20171225/20103333Etdypz7rhX.png
https://ithelp.ithome.com.tw/upload/images/20171225/2010333329epnZbT01.png

接著在# Fitting Algorithm步驟新增lreg2, Prediction步驟新增pred2, 作為預測結果, 程式碼修改如下:
https://ithelp.ithome.com.tw/upload/images/20171225/201033332ae08htsb7.png
https://ithelp.ithome.com.tw/upload/images/20171225/20103333h4XRtGgmhW.png

觀察一下pred2的預測結果, 似乎比pred好一些, 接著一樣在Evaluation步驟加入score2, 比較看看
https://ithelp.ithome.com.tw/upload/images/20171225/20103333vz5ZHuBRGK.png
https://ithelp.ithome.com.tw/upload/images/20171225/20103333SbbyGtQqss.png

score2似乎比score準確一點, 接著一樣來透過ggplot2做視覺化如下
https://ithelp.ithome.com.tw/upload/images/20171225/20103333KzxDD4l1BM.png

看起來好像有點樣子了, 來修改一下程式碼, 把前一篇的圖也放進來比較
https://ithelp.ithome.com.tw/upload/images/20171225/20103333o4vRAA7NTk.png
https://ithelp.ithome.com.tw/upload/images/20171225/20103333WKRCzIJq4T.png

一比較後明顯進步了, 所以用多項式感覺有點用, 接著加入Level的三次方與四次方跑看看, 資料如下
https://ithelp.ithome.com.tw/upload/images/20171225/20103333yQS1Hhhdre.png

由上圖可知四次方的score4是4個裡面最準確的, 接著一樣把四個圖放在一起,
https://ithelp.ithome.com.tw/upload/images/20171225/20103333O38sHcbgGF.png

我們可以發現四次方的圖, 很完美將所有點串在一起, 而這就是所謂的Overfitting, 而Overfitting會造成的問題是在訓練資料時很準, 但是測試資料非常不準, 要如何解決? 透過下一篇來總結Under fitting與 Overfitting囉

本文參考
Udemy Machine Learning A-Z™ Hands-On Python & R In Data Science- 06.Polynomial Regression
http://www.superdatascience.com/wp-content/uploads/2017/02/Polynomial_Regression.zip


上一篇
Day06 - Under fitting與 Overfitting Part1
下一篇
Day08 - Under fitting與 Overfitting Part3
系列文
Microsoft R Solution30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言